package com.example.demo.base;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

public class ArrayCountUtils {

    /**
     * 计算一个数组去重后的数量和每个去重值的数量
     * @param data 数据源
     * @return 返回的列表的长度代表去重后的数量   Group的字段title，count分别代表去重后的值和数量
     */
    public List<Group> find(String [] data) {

        //用来记录运算后的数据
        Map<String, Group> resultMap = new LinkedHashMap<>();
        /**
         * 运算到的位置记录
         */
        int countIndex = 0;

        while (countIndex < data.length) {

            String s = data[countIndex];
            //如果这个值运算过 不再运算
            if (resultMap.get(s) != null) {
                countIndex++;
                continue;
            }

            Group group = new Group();
            group.type = s;

            for (int i = countIndex; i < data.length; i++) {

                if (data[i].equals(s)) {
                    group.value++;
                }

            }
            resultMap.put(s, group);
            countIndex++;
        }


        return new ArrayList<Group>(resultMap.values());

    }

    /**
     * 将id进行合并nums, sums 相加道回合并后的集合使用Java8的流进行处理
     */
    public static List<BillsNums> merge(List<BillsNums> list) {
        Map<String, BillsNums> map = new HashMap<>();
        list.stream()
                .forEach(billsNums -> {
                    BillsNums last = map.get(billsNums.getId());
                    if(null != last){
                       // billsNums.setSums(billsNums.getSums() + last.getSums());
                        billsNums.setSums(add(billsNums.getSums(), last.getSums()));
                        map.put(billsNums.getId(), billsNums );
                    }else{
                        map.put(billsNums.getId(), billsNums);
                    }
                });
        return new ArrayList<>(map.values());

    }

    /**
     * 实现两个double类型的数据相加
     * @param v1
     * @param v2
     * @return
     */
    public static double add(double v1,double v2){
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.add(b2).doubleValue();
    }

    /**
     * 减法运算
     * @param m1
     * @param m2
     * @return
     */
    public static double subDouble(double m1, double m2) {
        BigDecimal p1 = new BigDecimal(Double.toString(m1));
        BigDecimal p2 = new BigDecimal(Double.toString(m2));
        return p1.subtract(p2).doubleValue();
    }

    /**
     * 用来记录去重后的信息
     */
    public class Group {
        public String type;
        public int value;

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

    }

    /**
     * 对象数组去重加属性合并
     */
    public static class BillsNums {
        private String id;
        private double sums;

        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public double getSums() {
            return sums;
        }
        public void setSums(double sums) {
            this.sums = sums;
        }
    }


    /**
     * 测试两个去重方法
     * @param args
     */
    public static void main(String args[]) {
        double a = 3.2;
        double b = 2.28;
        double sum = a-b;
        System.out.println(sum);

        String[] data = { "1", "2", "1", "1", "3", "asdf", "3", "asdf", "3", "asdf", "3", "asdf", "3", "asdf", "sss", "5" };

        ArrayCountUtils arrayCountUtils = new ArrayCountUtils();
        List<Group> groups = arrayCountUtils.find(data);
        for (Group group : groups) {

            System.out.println(group.type + ":::::"+group.value);

        }



        List<BillsNums> billsNumsList = new ArrayList<>();
        BillsNums billsNums = new BillsNums();
        billsNums.setId("1001");
        billsNums.setSums(1.1);
        billsNumsList.add(billsNums);

        BillsNums billsNums2 = new BillsNums();
        billsNums2.setId("1001");
        billsNums2.setSums(1.8);
        billsNumsList.add(billsNums2);

        List<BillsNums> result = merge(billsNumsList);
        System.out.println("result:" + result);
        for(BillsNums billNums : result){
            System.out.println(billNums.id + ":::::"+billNums.sums);
        }

    }

}